package leetcode.code2131;

import java.util.HashMap;
import java.util.Map;

import leetcode.IDebug;

public class Solution implements IDebug {

	public int longestPalindrome(String[] words) {
		int ans = 0;
		Map<String, Integer> cnts = new HashMap<>();
		int oo = 0;
		for (int i = 0; i < words.length; i++) {
			String cur = words[i];
			String pacur = new String(new char[] { cur.charAt(1), cur.charAt(0) });
			if (cnts.containsKey(pacur)) {
				ans += 4;
				int size = cnts.get(pacur);
				if (size > 1) {
					cnts.put(pacur, size - 1);
				} else {
					cnts.remove(pacur);
				}
				if (cur.charAt(1) == cur.charAt(0)) {
					oo--;
				}
			} else {
				int size = cnts.getOrDefault(cur, 0) + 1;
				cnts.put(cur, size);
				if (cur.charAt(1) == cur.charAt(0)) {
					oo++;
				}
			}
		}
		ans += oo > 0 ? 2 : 0;
		return ans;
	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
